﻿using Fetches.Custom.TestOrder;
using Fetches.Fetch.Dto;
using FetchesDeploy.Distributed.Factory;
using Microsoft.AspNetCore.Mvc;
using Serilog;

namespace FetchesDeploy.ScalableFactory.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class FetchController:ControllerBase
    {
        [HttpPost]
        public FetchReceipt Submit(FetchOrder order)
        {
            var task = ScalableFetchFactory.Instance.TakeFetchOrder(order);
            Log.Information("FetchFactory taken order {@order} returns receipt {@receipt}", order, task);
            return task;
        }

        [HttpPost("{taskId}")]
        public ActionResult Continue(long taskId)
        {
            ScalableFetchFactory.Instance.ContinueFetch(new FetchReceipt{Id=taskId});
            return Ok($"fetch {taskId} is continued");
        }

        [HttpDelete("{taskId}")]
        public ActionResult Stop(long taskId)
        {
            ScalableFetchFactory.Instance.StopFetch(new FetchReceipt { Id = taskId });
            return Ok($"fetch {taskId} is stopped");
        }

        [HttpGet("test")]
        public FetchReceipt TestFetch()
        {
            var order = WebMagicOrder.CreateForTest();
            return Submit(order);
        }
    }
}
